package http;

import main.robClass;
import okhttp3.*;

import java.util.*;
import java.util.concurrent.TimeUnit;

public class BasicHttpClient {
    private OkHttpClient client;
    //调整超时时间 免得线程过多的时候 或线路阻塞时候
    //服务端处理不及时 导致超时异常
    private final static int READ_TIMEOUT = 10;
    private final static int CONNECT_TIMEOUT = 10;
    private final static int WRITE_TIMEOUT = 10;
//
//    final static CookieJar cookieJar = new CookieJar() {
//        //cache
//            private final Map<String, List<Cookie>> cookiesMap = new HashMap<String, List<Cookie>>();
//        private int x = 0;
//
//        @Override
//        synchronized
//        public void saveFromResponse(HttpUrl httpUrl, List<Cookie> cookies) {
////            System.out.println("这是cookies"+cookies);
////            String host = httpUrl.host();
////            if (x > robClass.num) {
////                x = 0;
////            }
//            String tag;
//            System.out.println("httpUrl.uri().toString():" + httpUrl.uri().toString());
//            if (httpUrl.uri().toString().contains("xm=")) {
//                tag = httpUrl.uri().toString().split("xh=")[1].split("&")[0];
//                System.out.println("tag1:" + tag);
//            } else {
//                tag = httpUrl.uri().toString().split("xh=")[1];
//                System.out.println("tag2:" + tag);
//            }
//
////            System.out.println("查看传入tag" + tag);
////            List<Cookie> cookiesList = cookiesMap.get(tag);
////            System.out.println("保存的tag" + tag + "保存的cookiesList" +  cookiesList);
////            if (cookiesList != null) {
////                System.out.println("存在保存的tag" + tag + "存在保存的cookiesList" +  cookiesList);
////                cookiesMap.remove(tag);
////            }
//            cookiesMap.put(tag, cookies);
//            System.out.println("保存的tag" + tag + "保存的cookies" + cookies);
//            System.out.println("saveFromResponse-cookiesMap" + cookiesMap);
//        }
//
//        @Override
//        synchronized
//        public List<Cookie> loadForRequest(HttpUrl httpUrl) {
//
////            System.out.println("httpUrl.host()"+httpUrl.host());
////            System.out.println(httpUrl.uri().toString());
////            System.out.println(httpUrl.uri().toString().split("xh="));
////            System.out.println(httpUrl.uri().toString().split("xh=")[1]);
////            Random random = new Random();
////            int i = random.nextInt(robClass.num);
//            System.out.println("loadForRequest" + httpUrl.url().toString());
//            System.out.println("loadForRequest" + httpUrl.uri().toString());
//            String tag = null;
////            if (httpUrl.uri().toString().contains("xh=") ) {
////                tag = httpUrl.uri().toString().split("xh=")[1].split("&")[0] + "&" + i;
////            } else {
//                tag =  httpUrl.uri().toString();
////            }
//
//            System.out.println("loadForRequest读取的tag" + tag);
//
//            List<Cookie> cookiesList = cookiesMap.get(tag);
//            System.out.println("读取的cookiesList" + cookiesList);
//            System.out.println("cookies" + cookiesMap);
////            System.out.println("cookies"+ cookiesList);
//            return cookiesList != null ? cookiesList : new ArrayList<Cookie>();
//        }
//    };

    public BasicHttpClient() {
        if (client == null) {
            client = new OkHttpClient() //是否设置超时？ 感觉不要比较好？
                    .newBuilder()
//                    .cookieJar(cookieJar) //cookie persistence
                    .followRedirects(false)
                    .followSslRedirects(false)
                    .readTimeout(READ_TIMEOUT, TimeUnit.MINUTES)
                    .writeTimeout(WRITE_TIMEOUT, TimeUnit.MINUTES)
                    .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MINUTES)

//                    .retryOnConnectionFailure(true)
                    //最大空闲连接数 1000 持续时间10分钟
//                    .connectionPool(new ConnectionPool(10000,10,TimeUnit.MINUTES))
                    .build();
        }
    }

    public OkHttpClient getClient() {
        return client;
    }

    public void setClient(OkHttpClient client) {
        this.client = client;
    }

}

